﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FaWorksSolution.MvcApplication.Models.Sitemap;

namespace FaWorksSolution.MvcApplication.API.Controllers
{
    public class PageController : ApiControllerBase
    {
        // GET api/Page
        public IEnumerable<Page> GetPages()
        {
            var domainId = HttpContext.Current.Request.Params["domainId"];
            var pPageId = HttpContext.Current.Request.Params["pPageId"];
            var domainName = HttpContext.Current.Request.Params["domain"];

            if (!string.IsNullOrEmpty(domainId))
            {
                var domain = db.SitemapDomains.Include("Pages.Pages")
                    .FirstOrDefault(m => m.DomainId == new Guid(domainId));
                if (domain == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return domain.Pages.OrderBy(m => m.Order).AsEnumerable();
            }

            if (!string.IsNullOrEmpty(pPageId))
            {
                var page = db.SitemapPages.Include("Pages.Pages").FirstOrDefault(m => m.PageId == new Guid(pPageId));
                if (page == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return page.Pages.OrderBy(m => m.Order).AsEnumerable();
            }

            

            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }

        // GET api/Page/5
        public Page GetPage(Guid id)
        {
            Page page = db.SitemapPages.Find(id);
            if (page == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return page;
        }

        // PUT api/Page/5
        public HttpResponseMessage PutPage(Guid id, Page page)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != page.PageId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(page).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Page
        public HttpResponseMessage PostPage(Page page)
        {
            var domainId = HttpContext.Current.Request.Params["domainId"];
            var pPageId = HttpContext.Current.Request.Params["pPageId"];
            if (string.IsNullOrEmpty(domainId) && string.IsNullOrEmpty(pPageId))
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "缺少参数domainId或pageId");
            }

            if (ModelState.IsValid)
            {
                page.PageId = Guid.NewGuid();

                if (!string.IsNullOrEmpty(domainId))
                {
                    var domain = db.SitemapDomains.Find(new Guid(domainId));
                    domain.Pages = new List<Page>();
                    domain.Pages.Add(page);
                }
                if (!string.IsNullOrEmpty(pPageId))
                {
                    var pPage = db.SitemapPages.Find(new Guid(pPageId));
                    pPage.Pages = new List<Page>();
                    pPage.Pages.Add(page);
                }
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, page);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = page.PageId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Page/5
        public HttpResponseMessage DeletePage(Guid id)
        {
            Page page = db.SitemapPages.Find(id);
            if (page == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.SitemapPages.Remove(page);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, page);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private DefaultContext db = new DefaultContext();
    }
}